/*
 * @lc app=leetcode.cn id=88 lang=javascript
 *
 * [88] 合并两个有序数组
 */

// @lc code=start
/**
 * @param {number[]} nums1
 * @param {number} m
 * @param {number[]} nums2
 * @param {number} n
 * @return {void} Do not return anything, modify nums1 in-place instead.
 */
var merge = function (nums1, m, nums2, n) {
  let p1 = m - 1;
  let p2 = n - 1;
  let tail = m + n - 1;

  while (p1 > -1 || p2 > -1) {
    const val1 = nums1[p1];
    const val2 = nums2[p2];
    let temp;
    if (p2 === -1 || val1 > val2) {
      temp = nums1[p1--];
    } else if (p1 === -1 || val1 <= val2) {
      temp = nums2[p2--];
    }

    nums1[tail--] = temp;
  }
};
// @lc code=end
